package com.qfjy.project.weixin.api.tuling;

import com.qfjy.project.weixin.api.tuling.Bean.InputText;
import com.qfjy.project.weixin.api.tuling.Bean.Perception;
import com.qfjy.project.weixin.api.tuling.Bean.TulingBean;
import com.qfjy.project.weixin.api.tuling.Bean.UserInfo;
import com.qfjy.project.weixin.util.WeixinUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Component;

@Component
public class TulingUtil {
    private static final String TULING_POST_URL = "http://openapi.tuling123.com/openapi/api/v2";

    /**
     * @param InputMsg
     * @param apiKey
     * @return
     */
    public String beanConvertJson(String InputMsg, String apiKey) {
        TulingBean tulingBean = new TulingBean();
        tulingBean.setReqType(0);
        Perception p = new Perception();
        InputText inputText = new InputText();
        inputText.setText(InputMsg);
        p.setInputText(inputText);
        tulingBean.setPerception(p);
        UserInfo userInfo = new UserInfo();
        //"b487474670ab445cbd90a0f114133450"
        userInfo.setApiKey(apiKey);
        userInfo.setUserId("java");
        tulingBean.setUserInfo(userInfo);
        JSONObject jsonObject = JSONObject.fromObject(tulingBean);


        return jsonObject.toString();
    }


    public String sendUrl(String jsonstr) {
        JSONObject post = WeixinUtil.httpRequest(TULING_POST_URL, "POST", jsonstr);
        System.out.println(post);
        JSONArray jsonArray = (JSONArray) post.get("results");
        JSONObject J2 = (JSONObject) jsonArray.get(0);

        JSONObject J3 = (JSONObject) J2.get("values");
        String relut = J3.getString("text");
        System.out.println(relut);
        return relut;
    }

    public String sendMessage(String msg, String apikey) {
        String jsonstr = this.beanConvertJson(msg, apikey);

        String s = this.sendUrl(jsonstr);
        return s;
    }
//创建机器人数组


    public static String[] APIKEYS = {"768493348f674b59bd33532af96163f0", "bfe05586db914cc196017efbd927be28", "eb6cdf1f7a974bc7bdd6b35c39e60c7a", "a285d643902a4a00a546ded3495af4bf", "5d46f432d7a744dab2d0cdb7f5e5532b"};
    //方法send,指定下表索引
    private static int apikey_index = 0;
   //全局锁标识
    public static Boolean APIKEYS_LOCK_FLAG=true;
    public String send(String msg) {
      if(APIKEYS_LOCK_FLAG==false) {
          return "我今天累了";
      }
          String result = sendMessage(msg, APIKEYS[apikey_index]);
          if ("请求次数超限制！".equals(result)) {
              apikey_index++;
              if (apikey_index > APIKEYS.length) {
                  apikey_index = 0;
                  APIKEYS_LOCK_FLAG = false;
                  return "我累了";
              }
              return this.sendMessage(msg, APIKEYS[apikey_index]);
          }
          return result;


    }


//     public static void main(String[] args) {
//  //封装javabean
//  TulingBean tulingBean=new TulingBean();
//  tulingBean.setReqType(0);
//  Perception p=new Perception();
//  InputText inputText=new InputText();
//  inputText.setText("芜湖天气怎么样");
//   p.setInputText(inputText);
//  tulingBean.setPerception(p);
//  UserInfo userInfo=new UserInfo();
//  //"b487474670ab445cbd90a0f114133450"
//  userInfo.setApiKey("b487474670ab445cbd90a0f114133450");
//  userInfo.setUserId("java");
//  tulingBean.setUserInfo(userInfo);
//  JSONObject jsonObject = JSONObject.fromObject(tulingBean);
//  System.out.println(jsonObject);
//  JSONObject post = WeixinUtil.httpRequest(TULING_POST_URL, "POST", jsonObject.toString());
//  System.out.println(post);
//  JSONArray jsonArray = (JSONArray) post.get("results");
//  JSONObject  J2 = (JSONObject) jsonArray.get(0);
//
//  JSONObject J3 = (JSONObject) J2.get("values");
//  String relut=J3.getString("text");
//  System.out.println(relut);
//
// }
}